package com.liyunc.demo.comp.json.converter;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lombok.extern.log4j.Log4j2;

import java.io.IOException;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.Objects;

/**
 * @author liyuncong
 * @version 1.0
 * @file CustomJsonSerializer
 * @brief CustomJsonSerializer
 * @details CustomJsonSerializer
 * @date 2022/5/21
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                        NAME               DESCRIPTION
 * 2022/5/21                     liyuncong          Created
 */
@Log4j2
public class CustomJsonSerializer {

    public static final CustomDateJsonSerializer dateJsonSerializer =
        new CustomDateJsonSerializer();

    private static class CustomDateJsonSerializer extends JsonSerializer<LocalDateTime> {
        private final DateTimeFormatter format = DateTimeFormatter.ISO_OFFSET_DATE_TIME;

        @Override
        public void serialize(LocalDateTime value,
                              JsonGenerator generator,
                              SerializerProvider provider
        ) throws IOException {
            generator.writeString(format.format(value.atZone(ZoneId.systemDefault())));
        }
    }
}
